package com.syn.synold4s.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import androidx.core.content.ContextCompat;
import com.gaoyi.basemodule.baseAdapter.BaseAdapter;
import com.gaoyi.basemodule.baseAdapter.BaseViewHolder;
import com.syn.synold4s.R;
import com.syn.synold4s.bean.dto.MpReaderDto;
import com.syn.synold4s.databinding.Item360Binding;

public class Plant360Adapter extends BaseAdapter<Item360Binding, MpReaderDto> {
    private Integer deviceOnlineTotal;
    private Integer deviceTotal;
    private boolean isShowInfo = true;

    public Plant360Adapter(Context context) {
        super(context);
    }

    public void setAllDeviceCount(Integer deviceOnlineTotal,Integer deviceTotal){
        this.deviceOnlineTotal = deviceOnlineTotal;
        this.deviceTotal = deviceTotal;
    }

    @Override
    protected void setBindingVariable(Item360Binding binding, int position) {
    }

    @Override
    protected void onBindItem(Item360Binding binding, MpReaderDto item, int position) {
        if (position == 0) {
            binding.topLayout.setVisibility(View.VISIBLE);
            binding.tvAllDevice.setText(String.format(mContext.getString(R.string.all_device), deviceOnlineTotal, deviceTotal));
        }
//        binding.tv360Sn.setText(item.getName());
        binding.tv360Time.setText(item.getLastAt() != null ? item.getLastAt() : "");
        String icon = item.getIcon();
        Log.e(this.getClass().getSimpleName(), "onBindItem: "+icon);
        int plant_360_offline = R.mipmap.plant_360_offline;
        if (null != icon){
            if ("car_offline".equals(icon)){
                plant_360_offline = R.mipmap.plant_360_offline;
            } else if ("car_online".equals(icon)){
                plant_360_offline = R.mipmap.plant_360_online;
            }
        }
        binding.iv360.setImageResource(plant_360_offline);
        binding.tv360Name.setText(item.getName() != null ? item.getName() : "");
        binding.tv360Status.setText(item.getCurStateName());
        Drawable mDrawable = ContextCompat.getDrawable(mContext, R.drawable.oval_yellow_bg);
        if (mDrawable != null) {
            mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
        }
        binding.tv360Status.setCompoundDrawablesRelative(mDrawable, null, null, null);
//        binding.tv360AlertStatus.setText(item.get());
//        binding.tv360AlertTime.setText(String.format(mContext.getString(R.string.latest_alert_time), item.getAlarmTime() != null ? item.getAlarmTime() : ""));
        binding.tv360Address.setText(String.format(mContext.getString(R.string.address), item.getAddress() != null ? item.getAddress() : ""));

        binding.infoLayout.setVisibility(isShowInfo ? View.VISIBLE : View.GONE);
        binding.tvAllExpand.setTextColor(isShowInfo ? mContext.getColor(R.color.color_main) : mContext.getColor(R.color.color_text));
        Drawable expandDrawable = null;
        if (isShowInfo) {
            expandDrawable = ContextCompat.getDrawable(mContext, R.drawable.ic_expand);
        } else {
            expandDrawable = ContextCompat.getDrawable(mContext, R.drawable.ic_expand_close);
        }
        if (expandDrawable != null) {
            expandDrawable.setBounds(0, 0, expandDrawable.getIntrinsicWidth(), expandDrawable.getIntrinsicHeight());
        }
        binding.tvAllExpand.setCompoundDrawablesRelative(null, null, expandDrawable, null);
    }

    @Override
    protected void onCreateView(Item360Binding binding, BaseViewHolder<Item360Binding> holder) {
        binding.tvAllExpand.setOnClickListener(view -> {
            isShowInfo = !isShowInfo;
            notifyDataSetChanged();
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.item_360;
    }
}
